	package no.gnet.s10.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import no.gnet.s10.broker.Broker;
import no.gnet.s10.types.GenericTransferBean;
import no.gnet.s10.types.Player;
import no.gnet.util.Constants;
import no.gnet.util.ErrorMessage;
import no.gnet.util.S10Props;

/**
 * Servlet implementation class for Servlet: Controller
 *
 */
 public class Controller extends GNetController{

	 /* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public Controller() {
		super();
	}   	
	
	/* (non-Javadoc)
	 * @see javax.servlet.GenericServlet#init(javax.servlet.ServletConfig)
	 */
	public void init(ServletConfig servletconfig) throws ServletException {		
		super.init(servletconfig);
		Properties p = new Properties();
		try {
			p.load(servletconfig.getServletContext().getResourceAsStream("/s10.properties"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		servletconfig.getServletContext().setAttribute("s10props", p);
		this.servletconfig=servletconfig;
		System.out.println("Props: "+p);
		S10Props.getInstance().setProps(p);
		
		System.out.println("Props: "+S10Props.getInstance().getProps());
	}
	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
//			response.getWriter().write("Hello");
			if(!_checkActivated(request,response)){
				doError(request,response,"Din konto er ikke aktivert",
						"Du har mottatt en epost med en link til aktivering. Sjekk epost!");
			}
			else{
				String command = request.getParameter("cmd");
				handleRequest(request, response, command);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}  	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
//			response.getWriter().write("Hello");
			if(!_checkActivated(request,response)){
				doError(request,response,"Din konto er ikke aktivert",
						"Du har mottatt en epost med en link til aktivering. Sjekk epost!");
			}
			else{
				String command = request.getParameter("cmd");
				handleRequest(request, response, command);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}   	  	    
		
	
	private boolean _checkActivated(HttpServletRequest request, HttpServletResponse response){
		String handle = request.getRemoteUser();
		boolean activated = false;
		try {
			Player p = Broker.getBroker().getPlayer(handle);
			if(p==null){
				System.out.println("WARN: Player null, not found: "+handle);
				return false;
			}
			System.out.println("Activated: "+p.getHandle()+":"+p.getActivated());
			activated = p.getActivated()==Constants.PLAYER_ACTIVATED;
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return activated;
		
	}
	
}	